package com.vonchenchen.renderer;

/**
 * Created by  on 2021/1/24.
 */
public class FileDataProvider {

    private long mPtr;
    private final Object mLock;

    public FileDataProvider(){
        mLock = new Object();
        mPtr = FileProviderNative.creatNativeFileProvider();
    }

    public int initProvider(String fileName){
        synchronized (mLock) {
            if (mPtr != 0) {
                return FileProviderNative.initProvider(mPtr, fileName);
            }
        }
        return -1;
    }

    public int provideNextFrame(){
        synchronized (mLock) {
            if (mPtr != 0) {
                return FileProviderNative.provideNextFrame(mPtr);
            }
        }
        return -1;
    }

    public int registerRenderer(FrameRenderer renderer){
        if(renderer == null || renderer.getPtr() == 0){
            return -1;
        }
        synchronized (mLock) {
            if (mPtr != 0) {
                FileProviderNative.registerRenderer(mPtr, renderer.getPtr());
            }
        }
        return 0;
    }

    public void release(){
        synchronized (mLock) {
            if (mPtr != 0) {
                FileProviderNative.destoryNativeProvider(mPtr);
                mPtr = 0;
            }
        }
    }
}
